<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card shadow="hover">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="物资去向" prop="accept">
<!--              <el-input v-model="form.accept" placeholder="请输入物资去向"  />-->
              <treeselect
                v-model="form.parentId"
                :options="deptOptions"
                :normalizer="normalizer"
                @select="selectAccept"
                placeholder="请输入物资去向"
              />
            </el-form-item>
            <el-form-item label="联系人" prop="peopleName">
              <el-input v-model="form.peopleName" placeholder="请输入联系人"  />
            </el-form-item>
            <el-form-item label="联系电话" prop="peoplePhone">
              <el-input v-model="form.peoplePhone" placeholder="请输入联系电话"/>
            </el-form-item>
            <el-form-item label="操作类型" prop="operateType">
              <el-radio-group v-model="form.operateType">
                <el-radio :label=1><span style="font-weight: normal">出库</span></el-radio>
                <el-radio :label=0><span style="font-weight: normal">入库</span></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="物资名称" prop="goodId">
              <template>
                <el-select v-model="form.goodId" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    @click.native ="changeName(item)"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
            <el-form-item label="物资单位" prop="goodSize">
              <template>
                <el-select v-model="form.goodSize" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.size"
                    :label="item.type"
                    :value="item.size">
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
            <el-form-item label="物资数量" prop="goodNum">
              <el-input-number v-model="form.goodNum" @change="handleNumChange" :min="1" :max="10000" label="物资数量">1</el-input-number>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
            <div style="text-align:center">
              <el-button type="primary" @click="submitForm">提 交</el-button>
              <el-button @click="reset">重 置</el-button>
            </div>
          </el-form>

        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card shadow="hover">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="物资去向" prop="accept">
              <el-input
                v-model="queryParams.accept"
                placeholder="请输入"
                style="width: 120px"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="操作类型" prop="operateType">
              <el-select v-model="queryParams.operateType" placeholder="请选择" clearable style="width: 120px">
                <el-option
                  v-for="dict in dict.type.sys_job_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"

                />
              </el-select>
            </el-form-item>
            <el-form-item label="入库时间" prop="createTime">
              <el-date-picker clearable
                              style="width: 120px"
                              v-model="queryParams.createTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['good:goodstock:export']"
              >导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="goodstockList" @selection-change="handleSelectionChange">
            <el-table-column label="物资去向" align="center" prop="accept" />
            <el-table-column label="联系人" align="center" prop="peopleName" />
            <el-table-column label="操作类型" align="center" prop="operateType">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_job_status" :value="scope.row.operateType"/>
              </template>
            </el-table-column>
            <el-table-column label="物资名称" align="center" prop="goodName" />
            <el-table-column label="物资数量" align="center" prop="goodNum" />
            <el-table-column label="物资单位" align="center" prop="goodSize" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="入库时间" align="center" prop="createTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleView(scope.row)"
                >查看</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />

        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改物资出入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="dataForm" :rules="rules" label-width="80px">
        <el-form-item label="物资去向" prop="accept">
         {{dataForm.accept}}
        </el-form-item>
        <el-form-item label="联系人" prop="peopleName">
         {{dataForm.peopleName}}
        </el-form-item>
        <el-form-item label="联系电话" prop="peoplePhone">
          {{dataForm.peoplePhone}}
        </el-form-item>
        <el-form-item label="操作类型" prop="operateType">
          {{ dataForm.operateType === 0 ? '入库' : '出库' }}
        </el-form-item>
        <el-form-item label="物资名称" prop="goodName">
          {{dataForm.goodName}}
        </el-form-item>
        <el-form-item label="物资单位" prop="goodSize">
          {{dataForm.goodSize}}
        </el-form-item>
        <el-form-item label="物资数量" prop="goodNum">
          {{dataForm.goodNum}}
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          {{dataForm.remark}}
        </el-form-item>

      </el-form>

    </el-dialog>
  </div>
</template>

<script>
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import { listGoodstock, getGoodstock, delGoodstock, addGoodstock, updateGoodstock } from "@/api/good/goodstock";
  import { listGoodinfo } from "@/api/good/goodinfo";
  import {  listDept } from "@/api/system/dept";


  export default {
    name: "Goodstock",
    dicts: ['sys_job_status'],
    components: { Treeselect },
    data() {
      return {
        deptOptions: [],
        options: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 物资出入库表格数据
        goodstockList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          accept: null,
          createBy: null,
          goodNum: null,
          goodSize: null,
          goodName: null,
          peopleName: null,
          peoplePhone: null,
          operateType: null,
          createTime: null,
          updateTime: null,
        },

        dataForm:{},
        // 表单参数
        form: {
          operateType: 1,
          goodNum: 1,
          goodName: undefined,
        },
        // 表单校验
        rules: {
          accept: [
            { required: true, message: "物资去向不能为空", trigger: "blur" }
          ],
          peopleName: [
            { required: true, message: "联系人不能为空", trigger: "blur" }
          ],
          peoplePhone: [
            {
              required: true,
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur",
            },
          ],
          operateType: [
            { required: true,  trigger: "blur" }
          ],
          goodNum: [
            { required: true, trigger: "blur" }
          ],
          goodSize: [
            { required: true,  trigger: "blur" }
          ],
          goodId: [
            { required: true,  trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getInfoList();
      this.getDeptList();
    },
    methods: {
      /** 查询单元信息信息*/
      getDeptList(){
        listDept().then(res => {
          //console.log(res)
          this.deptOptions = this.handleTree(res.data, "deptId");
          // this.deptOptions = res.data
        })
      },
      selectAccept(node) {
        this.form.accept = node.deptName
        //console.log(this.form.accept)
      },
      /** 转换住宅数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children,
        };
      },
      /** 从info表中保存货物名字到stock表中*/
      changeName(item){
        //console.log(item)
        this.form.goodName = item.name;
      },
      /** 查询货物信息*/
      getInfoList(){
        listGoodinfo().then(res => {
          //console.log(res)
          this.options = res.rows;
        })
      },
      /** 计数器*/
      handleNumChange(value){
          this.goodNum = value;
      },
      /** 查询物资出入库列表 */
      getList() {
        this.loading = true;
        listGoodstock(this.queryParams).then(response => {
          this.goodstockList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          accept: null,
          createBy: null,
          goodNum: null,
          goodSize: null,
          goodId: null,
          peopleName: null,
          peoplePhone: null,
          operateType: 1,
          remark: null,
          createTime: null,
          updateTime: null,
          isDelete: null,
          version: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleView(row) {
        this.open = true
        this.dataForm = row
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
              addGoodstock(this.form).then(response => {
                this.$modal.msgSuccess("提交成功");
                this.open = false;
                this.getList();
                this.reset();
              });
          }
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('good/goodstock/export', {
          ...this.queryParams
        }, `goodstock_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
